// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import router from './router'
import moment from 'moment'

import VuePreview from 'vue-preview'

import '@/lib/mui/css/mui.min.css';
import '@/lib/mui/css/icons-extra.css';

//导入axios
import axios from 'axios'
Vue.prototype.$http = axios;
axios.defaults.baseURL = 'http://47.89.21.179:8080/';
axios.defaults.headers.common['Accept'] = 'application/json';
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';


//导入vue-resource
// import VueResource from 'vue-resource'

//安装vue-resource
// Vue.use(VueResource)
// Vue.http.options.root = 'http://47.89.21.179:8080/';

//全局配置 字符串拼接
// Vue.http.options.emulateJSON = true;

//安装moment
Vue.use(moment)
//全局过 滤器
Vue.filter('dateFormat',function(date,pattern="YYYY-MM-DD HH:mm:ss"){
  return moment(date).format(pattern)
})


//安装缩略图插件
Vue.use(VuePreview)
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {top: 0, bottom: 0},
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})

//按需加载
// import { Header,Swipe, SwipeItem ,Button,Lazyload} from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
 import MintUI from 'mint-ui'
 Vue.use(MintUI)
 import 'mint-ui/lib/style.css'
 

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car')||'[]')
var store = new Vuex.Store({
  state:{ //this.$store.state.***
    car:car
  },
  mutations:{ //this.$store.commit('方法名'，参数)
    //加入购物车
    addToCar(state,goodsinfo){
      var flag = false
      state.car.some(item=>{
        if(item.id == goodsinfo.id){
          item.count += goodsinfo.count
          flag = true
          return true
        }
      })
      if(!flag)
        state.car.push(goodsinfo)
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateCar(state,goodsinfo){
      state.car.forEach(item => {
        if(item.id == goodsinfo.id){
          item.count = goodsinfo.count
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    removeFromCar(state,id){
      state.car.some((item,i) => {
        if(item.id == id){
          state.car.splice(i,1)
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    changeSelected(state,id){
      state.car.some(item => {
        if(item.id == id){
          item.selected = !item.selected
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters:{ //this.$store.getters.***
    getAllCount(state){
      var c = 0
      state.car.some(item => {
        c += item.count
      });
      return c
    },
    getCount(state){
      var o = {}
      state.car.some(item => {
       o[item.id] = item.count
      })
      return o
    },
    getSelected(state){
      var o = {}
      state.car.some(item => {
        o[item.id] = item.selected
      })
      return o
    },
    getSum(state){
      var o = {count:0,sum:0}
      state.car.some(item => {
        if(item.selected){
          o.count += item.count
          o.sum += item.count*item.price
        }
      });
      return o
    } 
  }
})

Vue.config.productionTip = false

import App from './App'
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store
})
